<?php
namespace Admin\Model;
use Think\Model;

class TermModel extends Model {


    /**
     * 获取所有的学期信息
     * @return mixed
     */
    public function getAllTerm() {
        $res = $this->order('stime desc')->select();

        foreach ($res as $key=>$val) {
            $res[$key]['state_desc'] = $this->handlerState($val['state']);
        }

        return $res;
    }



    /**
     * 处理学期状态信息
     *
     * @param $state
     * @return string
     */
    protected function handlerState($state) {
        switch ($state) {
            case 0:
                return '<span class="label">已结束</span>';
            case 1:
                return '<span class="label label-success">正在进行</span>';
            case 2:
                return '<span class="label label-info">未到达</span>';
        }
        return $state;
    }


    /**
     * @param $id
     * @return mixed
     */
    public function getTermById($id) {
        return $this->find($id);
    }

    /**
     * 获取当前学期
     *
     * @return bool|mixed
     */
    public function getNowTerm() {
        $map['state'] = 1;
        $res = $this->where($map)->find();

        if (empty($res)) return false;
        return $res;
    }

    /**
     * 添加学期
     *
     * @param $data
     * @return string
     */
    public function addTerm($data) {
        if (!$this->checkRepeatTerm($data['stime'],$data['etime'])) return '100101';

        if (($res = $this->add($data)) !== false) return '000000';
        return '111111';
    }

    /**
     * 检查是否存在重复的学期
     *
     * @param $stime
     * @param $etime
     * @return bool
     */
    protected function checkRepeatTerm($stime,$etime) {

        $sy = date('Y',strtotime($stime));
        $ey = date('Y',strtotime($etime));

        $result = $this->select();
        foreach ($result as $val) {
            $_sy = date('Y',strtotime($val['stime']));
            $_ey = date('Y',strtotime($val['etime']));

            if ($sy == $_sy && $ey == $_ey) {
                return false;
                die;
            }
        }
        return true;
    }


    /**
     * 刷新学期状态
     */
    public function refreshState() {
        $res = $this->select();
        $time = time();

        foreach ($res as $val) {
            $stime = strtotime($val[stime]);
            $etime = strtotime($val[etime]);

            if ($stime <= $time || $etime >= $time)
                $this->where('id = '.$val['id'])->setField('state',1);
            if ($etime < $time)
                $this->where('id = '.$val['id'])->setField('state',0);
            if ($stime > $time)
                $this->where('id = '.$val['id'])->setField('state',2);
        }
        return;
    }


    public function editTerm($data) {
        $flag = $this->save($data);
        if ($flag !== false) return '000000';
        return '111111';
    }


    /**
     * 删除学期
     *
     * @param $id
     * @return bool
     */
    public function delTerm($id) {
        $t = $this->find($id);
        if ($t['state'] === '1') return '100102';
        if (($flag = $this->delete($id)) !== false) return '000000';
        return '111111';
    }

}